﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;

namespace web.component
{
    public class Ustring
    {
        #region html util
        /// <summary>
        /// Hàm của YAF - chuan hoa chuoi
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static string CleanStringForURL(string str)
        {
            StringBuilder sb = new StringBuilder();

            // trim...
            str = HttpContext.Current.Server.HtmlDecode(str.Trim());

            // fix ampersand...
            str = str.Replace("&", string.Empty);//"and");
            //Modified by: MrSlowly - Aug-26-2009
            str = Regex.Replace(str, "Đ|đ|&#273;|&#272;", "d", RegexOptions.IgnoreCase);
            // normalize the Unicode
            str = str.Normalize(NormalizationForm.FormKD);

            for (int i = 0; i < str.Length; i++)
            {
                if (char.IsWhiteSpace(str[i])) sb.Append('_');
                else if (char.GetUnicodeCategory(str[i]) != UnicodeCategory.NonSpacingMark
                    && !char.IsPunctuation(str[i])
                    && !char.IsSymbol(str[i])
                    )
                    sb.Append(str[i]);
            }

            return sb.ToString();
        }

        /// <summary>
        /// Strips all illegal characters from the specified title.
        /// </summary>
        public static string RemoveIllegalCharacters(string text)
        {
            if (string.IsNullOrEmpty(text))
                return text;

            text = CleanStringForURL(text);

            //text = text.Replace(":", string.Empty);
            //text = text.Replace("/", string.Empty);
            //text = text.Replace("?", string.Empty);
            //text = text.Replace("#", string.Empty);
            //text = text.Replace("[", string.Empty);
            //text = text.Replace("]", string.Empty);
            //text = text.Replace("@", string.Empty);
            //text = text.Replace("*", string.Empty);
            //text = text.Replace(".", string.Empty);
            //text = text.Replace(",", string.Empty);
            //text = text.Replace("\"", string.Empty);
            //text = text.Replace("&", string.Empty);
            //text = text.Replace("'", string.Empty);
            //text = Regex.Replace(text, "Đ|đ|&#273;|&#272;", "d", RegexOptions.IgnoreCase);
            //text = text.Replace(" ", "_");
            text = RemoveDiacritics(text);
            text = RemoveExtraHyphen(text);
            text = HttpUtility.UrlEncode(text).Replace("%", string.Empty);
            return text.ToLower();
        }

        public static string RemoveExtraHyphen(string text)
        {
            if (text.Contains("__"))
            {
                text = text.Replace("__", "_");
                return RemoveExtraHyphen(text);
            }

            return text;
        }

        private static String RemoveDiacritics(string text)
        {
            String normalized = text.Normalize(NormalizationForm.FormD);
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < normalized.Length; i++)
            {
                Char c = normalized[i];
                if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
                    sb.Append(c);
            }

            return sb.ToString();
        }

        private static readonly Regex STRIP_HTML = new Regex("<[^>]*>", RegexOptions.Compiled);
        /// <summary>
        /// Strips all HTML tags from the specified string.
        /// </summary>
        /// <param name="html">The string containing HTML</param>
        /// <returns>A string without HTML tags</returns>
        public static string StripHtml(string html)
        {
            if (string.IsNullOrEmpty(html))
                return string.Empty;

            return STRIP_HTML.Replace(html, string.Empty);
        }

        private static readonly Regex REGEX_BETWEEN_TAGS = new Regex(@">\s+", RegexOptions.Compiled);
        private static readonly Regex REGEX_LINE_BREAKS = new Regex(@"\n\s+", RegexOptions.Compiled);

        /// <summary>
        /// Removes the HTML whitespace.
        /// </summary>
        /// <param name="html">The HTML.</param>
        public static string RemoveHtmlWhitespace(string html)
        {
            if (string.IsNullOrEmpty(html))
                return string.Empty;

            html = REGEX_BETWEEN_TAGS.Replace(html, "> ");
            html = REGEX_LINE_BREAKS.Replace(html, string.Empty);

            return html.Trim();
        }

        ///// <summary>
        ///// Writes ETag and Last-Modified headers and sets the conditional get headers.
        ///// </summary>
        ///// <param name="date">The date.</param>
        //public static bool SetConditionalGetHeaders(DateTime date)
        //{

        //    // SetLastModified() below will throw an error if the 'date' is a future date.
        //    if (date > DateTime.Now)
        //        date = DateTime.Now;

        //    HttpResponse response = HttpContext.Current.Response;
        //    HttpRequest request = HttpContext.Current.Request;

        //    string etag = "\"" + date.Ticks + "\"";
        //    string incomingEtag = request.Headers["If-None-Match"];

        //    response.AppendHeader("ETag", etag);
        //    response.Cache.SetLastModified(date);

        //    if (String.Compare(incomingEtag, etag) == 0)
        //    {
        //        response.Clear();
        //        response.StatusCode = (int)System.Net.HttpStatusCode.NotModified;
        //        return true;
        //    }

        //    return false;
        //}

        #endregion

        #region string display
        //lay ten san pham
        public static string GetProductName(string str)
        {
            try
            {
                return cutString(str, 25);
            }
            catch
            {
                return "";// Lang.GetLienHe(Util.GetLang());
            }
        }
        //Hien thi tien 
        public static string GetMoney(object money)
        {
            try
            {
                if (Convert.ToDouble(money) <= 0)
                {
                    return "Giá: liên hệ"; //Uxmlhelper.GetTranslate("price_contact");
                }
                else
                {
                    return string.Format(System.Globalization.CultureInfo.CreateSpecificCulture("vi-VN"), "Giá: {0:c0}", Convert.ToDouble(money));
                }
            }
            catch
            {
                return "Giá: liên hệ";//Uxmlhelper.GetTranslate("price_contact");
            }
        }
        //Hien thi tien 
        public static string GetMoney(object g1, object g2)
        {
            if (Convert.ToDecimal(g1) > 0)
            {
                if (Convert.ToDecimal(g2) > 0)
                    return string.Format("<span class='price-old'>{0}</span> <span class='price-new'>{1}</span>",
                        GetMoney(g1), GetMoney(g2));
                else
                    return GetMoney(g2);
            }
            else
                return GetMoney(g2);
        }

        //Hien thi tien 
        public static string GetMoney2(object money)
        {
            try
            {
                if (Convert.ToDouble(money) <= 0)
                {
                    return "liên hệ"; //Uxmlhelper.GetTranslate("price_contact");
                }
                else
                {
                    return string.Format(System.Globalization.CultureInfo.CreateSpecificCulture("vi-VN"), "{0:c0}", Convert.ToDouble(money));
                }
            }
            catch
            {
                return "liên hệ";//Uxmlhelper.GetTranslate("price_contact");
            }
        }
        // con hang- het hang
        public static string GetProductStatus(object p_qty)
        {
            if ((int)p_qty > 0) { 
                return "Còn hàng"; 
            } else {
                return "Hết hàng";
            }
        }

        /// <summary>
        /// Hien thi ngay : dd/MM/yyyy
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string GetDate(object date)
        {
            try
            {
                DateTime dt = Convert.ToDateTime(date);
                return dt.ToString(global_const.CS_DISPLAY_DATE_FORMAT);
            }
            catch
            {
                return "";// Lang.GetLienHe(Util.GetLang());
            }
        }
        /// <summary>
        /// lay ngay gio : dd/MM/yyyy HH:mm
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string GetDateTime(object date)
        {
            try
            {
                DateTime dt = Convert.ToDateTime(date);
                return dt.ToString(global_const.CS_DISPLAY_DATETIME_FORMAT);
            }
            catch
            {
                return "";// Lang.GetLienHe(Util.GetLang());
            }
        }

        //lay trang thai don hang
        public static string GetOrderStatus(int status)
        {
            try
            {
                switch (status) { 
                    case (int)global_const.E_PAYMENT_STATUS.START:
                        return "Chờ xử lý";
                    case (int)global_const.E_PAYMENT_STATUS.PROCESS:
                        return "Đang xử lý";
                    case (int)global_const.E_PAYMENT_STATUS.SHIPPING:
                        return "Đang chuyển hàng";
                    case (int)global_const.E_PAYMENT_STATUS.SUCCESS:
                        return "Giao dịch thành công";
                    default:
                        return "";
                }
                
            }
            catch
            {
                return "###";// Lang.GetLienHe(Util.GetLang());
            }
        }
        #endregion

        #region ham xử lý string

        public string RandomString(int size, bool lowerCase)
        {
            StringBuilder builder = new StringBuilder();
            Random random = new Random();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }
            if (lowerCase)
                return builder.ToString().ToLower();
            return builder.ToString();
        }

        public int RandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }

        public string GetCodeRamdom()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(RandomString(2, true));
            builder.Append(RandomNumber(100000, 999999));
            return builder.ToString();
        }

        public static string cutString(string input, int characterLimit)
        {
            string output = input;
            if (output.Length > characterLimit && characterLimit > 0)
            {
                output = output.Substring(0, characterLimit);
                if (input.Substring(output.Length, 1) != " ")
                {
                    int LastSpace = output.LastIndexOf(" ");
                    if (LastSpace != -1)
                    {
                        output =
                            output.Substring(0, LastSpace);
                    }
                }
                output += "...";
            }
            return output;
        }

        public virtual string ConvertString(string value) // Chuyển có dấu thành không dấu
        {
            value = value.Replace(':', ' ');
            value = value.Replace('/', ' ');

            value = value.Replace('\"', ' ');
            value = value.Replace('\"', ' ');
            value = value.Replace('%', ' ');
            value = value.Replace('@', ' ');
            value = value.Replace('$', ' ');
            value = value.Replace('#', ' ');
            value = value.Replace('^', ' ');
            value = value.Replace('&', ' ');
            value = value.Replace('!', ' ');
            value = value.Replace('*', ' ');

            value = value.Replace("ấ", "a");
            value = value.Replace("ầ", "a");
            value = value.Replace("ẩ", "a");
            value = value.Replace("ẫ", "a");
            value = value.Replace("ậ", "a");

            value = value.Replace("Ấ", "A");
            value = value.Replace("Ầ", "A");
            value = value.Replace("Ẩ", "A");
            value = value.Replace("Ẫ", "A");
            value = value.Replace("Ậ", "A");

            value = value.Replace("ắ", "a");
            value = value.Replace("ằ", "a");
            value = value.Replace("ẳ", "a");
            value = value.Replace("ẵ", "a");
            value = value.Replace("ặ", "a");

            value = value.Replace("Ắ", "A");
            value = value.Replace("Ằ", "A");
            value = value.Replace("Ẳ", "A");
            value = value.Replace("Ẵ", "A");
            value = value.Replace("Ặ", "A");

            value = value.Replace("á", "a");
            value = value.Replace("à", "a");
            value = value.Replace("ả", "a");
            value = value.Replace("ã", "a");
            value = value.Replace("ạ", "a");
            value = value.Replace("â", "a");
            value = value.Replace("ă", "a");

            value = value.Replace("Á", "A");
            value = value.Replace("À", "A");
            value = value.Replace("Ả", "A");
            value = value.Replace("Ã", "A");
            value = value.Replace("Ạ", "A");
            value = value.Replace("Â", "A");
            value = value.Replace("Ă", "A");

            value = value.Replace("ế", "e");
            value = value.Replace("ề", "e");
            value = value.Replace("ể", "e");
            value = value.Replace("ễ", "e");
            value = value.Replace("ệ", "e");

            value = value.Replace("Ế", "E");
            value = value.Replace("Ề", "E");
            value = value.Replace("Ể", "E");
            value = value.Replace("Ễ", "E");
            value = value.Replace("Ệ", "E");

            value = value.Replace("é", "e");
            value = value.Replace("è", "e");
            value = value.Replace("ẻ", "e");
            value = value.Replace("ẽ", "e");
            value = value.Replace("ẹ", "e");
            value = value.Replace("ê", "e");

            value = value.Replace("É", "E");
            value = value.Replace("È", "E");
            value = value.Replace("Ẻ", "E");
            value = value.Replace("Ẽ", "E");
            value = value.Replace("Ẹ", "E");
            value = value.Replace("Ê", "E");

            value = value.Replace("í", "i");
            value = value.Replace("ì", "i");
            value = value.Replace("ỉ", "i");
            value = value.Replace("ĩ", "i");
            value = value.Replace("ị", "i");

            value = value.Replace("Í", "I");
            value = value.Replace("Ì", "I");
            value = value.Replace("Ỉ", "I");
            value = value.Replace("Ĩ", "I");
            value = value.Replace("Ị", "I");

            value = value.Replace("ố", "o");
            value = value.Replace("ồ", "o");
            value = value.Replace("ổ", "o");
            value = value.Replace("ỗ", "o");
            value = value.Replace("ộ", "o");

            value = value.Replace("Ố", "O");
            value = value.Replace("Ồ", "O");
            value = value.Replace("Ổ", "O");
            value = value.Replace("Ô", "O");
            value = value.Replace("Ộ", "O");

            value = value.Replace("ớ", "o");
            value = value.Replace("ờ", "o");
            value = value.Replace("ở", "o");
            value = value.Replace("ỡ", "o");
            value = value.Replace("ợ", "o");

            value = value.Replace("Ớ", "O");
            value = value.Replace("Ờ", "O");
            value = value.Replace("Ở", "O");
            value = value.Replace("Ỡ", "O");
            value = value.Replace("Ợ", "O");

            value = value.Replace("ứ", "u");
            value = value.Replace("ừ", "u");
            value = value.Replace("ử", "u");
            value = value.Replace("ữ", "u");
            value = value.Replace("ự", "u");

            value = value.Replace("Ứ", "U");
            value = value.Replace("Ừ", "U");
            value = value.Replace("Ử", "U");
            value = value.Replace("Ữ", "U");
            value = value.Replace("Ự", "U");

            value = value.Replace("ý", "y");
            value = value.Replace("ỳ", "y");
            value = value.Replace("ỷ", "y");
            value = value.Replace("ỹ", "y");
            value = value.Replace("ỵ", "y");

            value = value.Replace("Ý", "Y");
            value = value.Replace("Ỳ", "Y");
            value = value.Replace("Ỷ", "Y");
            value = value.Replace("Ỹ", "Y");
            value = value.Replace("Ỵ", "Y");

            value = value.Replace("Đ", "D");
            value = value.Replace("Đ", "D");
            value = value.Replace("đ", "d");

            value = value.Replace("ó", "o");
            value = value.Replace("ò", "o");
            value = value.Replace("ỏ", "o");
            value = value.Replace("õ", "o");
            value = value.Replace("ọ", "o");
            value = value.Replace("ô", "o");
            value = value.Replace("ơ", "o");

            value = value.Replace("Ó", "O");
            value = value.Replace("Ò", "O");
            value = value.Replace("Ỏ", "O");
            value = value.Replace("Õ", "O");
            value = value.Replace("Ọ", "O");
            value = value.Replace("Ô", "O");
            value = value.Replace("Ơ", "O");

            value = value.Replace("ú", "u");
            value = value.Replace("ù", "u");
            value = value.Replace("ủ", "u");
            value = value.Replace("ũ", "u");
            value = value.Replace("ụ", "u");
            value = value.Replace("ư", "u");

            value = value.Replace("Ú", "U");
            value = value.Replace("Ù", "U");
            value = value.Replace("Ủ", "U");
            value = value.Replace("Ũ", "U");
            value = value.Replace("Ụ", "U");
            value = value.Replace("Ư", "U");

            value = value.Replace(" ", "-");
            value = value.Replace(".", "-");
            value = value.Replace(",", "-");
            value = value.Replace("(", "-");
            value = value.Replace(")", "-");
            value = value.Replace("+", "-");
            value = value.Replace("=", "-");
            value = value.Replace("~", "-");
            value = value.Replace("`", "-");
            value = value.Replace("[", "-");
            value = value.Replace("]", "-");
            value = value.Replace("_", "-");

            return value;
        }

        public virtual string ConvertString2(string value) // Chuyển có dấu thành không dấu
        {
            value = value.Replace(':', ' ');
            value = value.Replace('/', ' ');

            value = value.Replace('\"', ' ');
            value = value.Replace('\"', ' ');
            value = value.Replace('%', ' ');
            value = value.Replace('@', ' ');
            value = value.Replace('$', ' ');
            value = value.Replace('#', ' ');
            value = value.Replace('^', ' ');
            value = value.Replace('&', ' ');
            value = value.Replace('!', ' ');
            value = value.Replace('*', ' ');

            value = value.Replace("ấ", "a");
            value = value.Replace("ầ", "a");
            value = value.Replace("ẩ", "a");
            value = value.Replace("ẫ", "a");
            value = value.Replace("ậ", "a");

            value = value.Replace("Ấ", "A");
            value = value.Replace("Ầ", "A");
            value = value.Replace("Ẩ", "A");
            value = value.Replace("Ẫ", "A");
            value = value.Replace("Ậ", "A");

            value = value.Replace("ắ", "a");
            value = value.Replace("ằ", "a");
            value = value.Replace("ẳ", "a");
            value = value.Replace("ẵ", "a");
            value = value.Replace("ặ", "a");

            value = value.Replace("Ắ", "A");
            value = value.Replace("Ằ", "A");
            value = value.Replace("Ẳ", "A");
            value = value.Replace("Ẵ", "A");
            value = value.Replace("Ặ", "A");

            value = value.Replace("á", "a");
            value = value.Replace("à", "a");
            value = value.Replace("ả", "a");
            value = value.Replace("ã", "a");
            value = value.Replace("ạ", "a");
            value = value.Replace("â", "a");
            value = value.Replace("ă", "a");

            value = value.Replace("Á", "A");
            value = value.Replace("À", "A");
            value = value.Replace("Ả", "A");
            value = value.Replace("Ã", "A");
            value = value.Replace("Ạ", "A");
            value = value.Replace("Â", "A");
            value = value.Replace("Ă", "A");

            value = value.Replace("ế", "e");
            value = value.Replace("ề", "e");
            value = value.Replace("ể", "e");
            value = value.Replace("ễ", "e");
            value = value.Replace("ệ", "e");

            value = value.Replace("Ế", "E");
            value = value.Replace("Ề", "E");
            value = value.Replace("Ể", "E");
            value = value.Replace("Ễ", "E");
            value = value.Replace("Ệ", "E");

            value = value.Replace("é", "e");
            value = value.Replace("è", "e");
            value = value.Replace("ẻ", "e");
            value = value.Replace("ẽ", "e");
            value = value.Replace("ẹ", "e");
            value = value.Replace("ê", "e");

            value = value.Replace("É", "E");
            value = value.Replace("È", "E");
            value = value.Replace("Ẻ", "E");
            value = value.Replace("Ẽ", "E");
            value = value.Replace("Ẹ", "E");
            value = value.Replace("Ê", "E");

            value = value.Replace("í", "i");
            value = value.Replace("ì", "i");
            value = value.Replace("ỉ", "i");
            value = value.Replace("ĩ", "i");
            value = value.Replace("ị", "i");

            value = value.Replace("Í", "I");
            value = value.Replace("Ì", "I");
            value = value.Replace("Ỉ", "I");
            value = value.Replace("Ĩ", "I");
            value = value.Replace("Ị", "I");

            value = value.Replace("ố", "o");
            value = value.Replace("ồ", "o");
            value = value.Replace("ổ", "o");
            value = value.Replace("ỗ", "o");
            value = value.Replace("ộ", "o");

            value = value.Replace("Ố", "O");
            value = value.Replace("Ồ", "O");
            value = value.Replace("Ổ", "O");
            value = value.Replace("Ô", "O");
            value = value.Replace("Ộ", "O");

            value = value.Replace("ớ", "o");
            value = value.Replace("ờ", "o");
            value = value.Replace("ở", "o");
            value = value.Replace("ỡ", "o");
            value = value.Replace("ợ", "o");

            value = value.Replace("Ớ", "O");
            value = value.Replace("Ờ", "O");
            value = value.Replace("Ở", "O");
            value = value.Replace("Ỡ", "O");
            value = value.Replace("Ợ", "O");

            value = value.Replace("ứ", "u");
            value = value.Replace("ừ", "u");
            value = value.Replace("ử", "u");
            value = value.Replace("ữ", "u");
            value = value.Replace("ự", "u");

            value = value.Replace("Ứ", "U");
            value = value.Replace("Ừ", "U");
            value = value.Replace("Ử", "U");
            value = value.Replace("Ữ", "U");
            value = value.Replace("Ự", "U");

            value = value.Replace("ý", "y");
            value = value.Replace("ỳ", "y");
            value = value.Replace("ỷ", "y");
            value = value.Replace("ỹ", "y");
            value = value.Replace("ỵ", "y");

            value = value.Replace("Ý", "Y");
            value = value.Replace("Ỳ", "Y");
            value = value.Replace("Ỷ", "Y");
            value = value.Replace("Ỹ", "Y");
            value = value.Replace("Ỵ", "Y");

            value = value.Replace("Đ", "D");
            value = value.Replace("Đ", "D");
            value = value.Replace("đ", "d");

            value = value.Replace("ó", "o");
            value = value.Replace("ò", "o");
            value = value.Replace("ỏ", "o");
            value = value.Replace("õ", "o");
            value = value.Replace("ọ", "o");
            value = value.Replace("ô", "o");
            value = value.Replace("ơ", "o");

            value = value.Replace("Ó", "O");
            value = value.Replace("Ò", "O");
            value = value.Replace("Ỏ", "O");
            value = value.Replace("Õ", "O");
            value = value.Replace("Ọ", "O");
            value = value.Replace("Ô", "O");
            value = value.Replace("Ơ", "O");

            value = value.Replace("ú", "u");
            value = value.Replace("ù", "u");
            value = value.Replace("ủ", "u");
            value = value.Replace("ũ", "u");
            value = value.Replace("ụ", "u");
            value = value.Replace("ư", "u");

            value = value.Replace("Ú", "U");
            value = value.Replace("Ù", "U");
            value = value.Replace("Ủ", "U");
            value = value.Replace("Ũ", "U");
            value = value.Replace("Ụ", "U");
            value = value.Replace("Ư", "U");

            value = value.Replace(" ", "-");
            value = value.Replace(",", "-");
            value = value.Replace("(", "-");
            value = value.Replace(")", "-");
            value = value.Replace("+", "-");
            value = value.Replace("=", "-");
            value = value.Replace("~", "-");
            value = value.Replace("`", "-");
            value = value.Replace("[", "-");
            value = value.Replace("]", "-");
            value = value.Replace("_", "-");

            return value;
        }

        // lay kieu du lieu id cua enum tra ve string
        public static string GetEnumIdToString(object obj) {
          return   Convert.ToString((int)obj);
        }
        #endregion

        #region chuyển chuỗi số (tiền) thành chữ
        private string[] onesMapping =
          new string[] {
                    "Không", "Một", "Hai", "Ba", "Bốn", "Năm", "Sáu", "Bảy", "Tám", "Chín"
                };
        private static string[] tensMapping =
            new string[] {
                    "Mười","Hai Mươi", "Ba mươi", "Bốn mươi", "Năm mươi", "Sáu mươi", "Bảy mươi", "Tám mươi", "Chín mươi"
                };
        private static string[] groupMapping =
            new string[] {
                   "Trăm", "Nghìn","Triệu", "Tỉ", "Nghìn", "Triệu", "Tỉ", "Nghìn", "Triệu", "Tỉ"
                };
        public virtual string convertNumberToChar(int number)
        {
            return convertNumberToChar((long)number);
        }

        public string convertNumberToChar(long number)
        {
            //if (number == 0)
            //{
            //    return onesMapping[number];
            //}

            //string sign = string.Empty;
            //if (number < 0)
            //{
            //    sign = "Âm";
            //    number = Math.Abs(number);
            //}

            //string retVal = null;
            //int group = 0;
            //while (number > 0)
            //{
            //    int numberToProcess = (int)(number % 1000);
            //    number = number / 1000;

            //    string groupDescription = ProcessGroup(numberToProcess);
            //    if (groupDescription != null)
            //    {
            //        if (group > 0)
            //        {
            //            retVal = groupMapping[group] + " " + retVal;
            //        }
            //        retVal = groupDescription + " " + retVal;
            //    }
            //    group++;
            //}

            string sign = string.Empty;
            if (number < 0)
            {
                sign = "Âm";
                number = Math.Abs(number);
            }
            string retVal = null;
            retVal = convertNumberToChar(number.ToString());
            return sign + " " + retVal;
        }

        private string ProcessGroup(int number)
        {
            int tens = number % 100;
            int hundreds = number / 100;

            string retVal = null;
            if (hundreds > 0)
            {
                retVal = onesMapping[hundreds] + " " + groupMapping[0];
            }
            if (tens > 0)
            {
                if (tens < 10)
                {
                    retVal += ((retVal != null) ? " Lẻ " : string.Empty) + onesMapping[tens];
                }
                else
                {
                    int ones = tens % 10;
                    tens = (tens / 10) - 1; // 20's offset

                    retVal += ((retVal != null) ? " " : string.Empty) + tensMapping[tens];

                    if (ones > 0)
                    {
                        retVal += ((retVal != null) ? " " : string.Empty) + onesMapping[ones];
                    }
                }
            }
            return retVal;
        }

        public virtual string convertNumberToChar(string s)
        {
            string[] so = {"không", "một", "hai", "ba", "bốn", "năm", "sáu", "bảy", "tám", "chín"
             };
            string[] hang = { string.Empty, "nghìn", "triệu", "tỉ" };

            int i;
            int j;
            int donvi;
            int chuc;
            int tram;
            string str;
            str = string.Empty;
            i = s.Length;
            if (s == "0")
            {
                return "không";
            }
            ///''''
            i = s.Length;
            if (i == 0)
            {
                str = string.Empty;
            }
            else
            {
                j = 0;
                while (i > 0)
                {
                    donvi = Int32.Parse(s.Substring(i - 1, 1));
                    i = i - 1;
                    if (i > 0)
                    {
                        chuc = Int32.Parse(s.Substring(i - 1, 1));
                    }
                    else
                    {
                        chuc = -1;
                    }
                    i = i - 1;
                    if (i > 0)
                    {
                        tram = Int32.Parse(s.Substring(i - 1, 1));
                    }
                    else
                    {
                        tram = -1;
                    }
                    i = i - 1;
                    if (donvi > 0 | chuc > 0 | tram > 0 | j == 3)
                    {
                        str = hang[j] + " " + str;
                    }
                    j = j + 1;
                    if (j > 3)
                    {
                        j = 1;
                    }
                    if (donvi == 1 & chuc > 1)
                    {
                        str = "mốt" + " " + str;
                    }
                    else
                    {
                        if (donvi == 5 & chuc > 0)
                        {
                            str = "lăm" + " " + str;
                        }
                        else if (donvi > 0)
                        {
                            str = so[donvi] + " " + str;
                        }
                    }
                    if (chuc < 0)
                    {
                        break;
                    }
                    else
                    {
                        if (chuc == 0 & donvi > 0)
                        {
                            str = "lẻ" + " " + str;
                        }
                        else if (chuc == 1)
                        {
                            str = "mười" + " " + str;
                        }
                        else if (chuc > 1)
                        {
                            str = so[chuc] + " " + "mươi" + " " + str;
                        }
                    }
                    if (tram < 0)
                    {
                        break;
                    }
                    else
                    {
                        if (tram > 0 | chuc > 0 | donvi > 0)
                        {
                            str = so[tram] + " " + "trăm" + " " + str;
                        }
                    }
                }
            }
            return str.Trim();
        }
        #endregion


    }
}
